<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>getDerivedStateFromProps</title>
</head>

<body>
    <div id="app"></div>

    <script src="../../00_课程资料/js@17.0.2/react.development.js"></script>
    <script src="../../00_课程资料/js@17.0.2/react-dom.development.js"></script>
    <script src="../../00_课程资料/js@17.0.2/babel.min.js"></script>

    <script type="text/babel">
        class Count extends React.Component{
            constructor(props){
                super(props)
                console.log('constructor');
            }
            state = {
                count:19
            }
            addCount = () =>{
                let {count} = this.state
                this.setState({
                    count:++count
                })
            }
            // 17.x新增声明周期函数，从props中获取衍生过来的状态
            // 这个函数必须返回null或者衍生状态对象，返回衍生状态对象后，该状态值不能被修改
            static getDerivedStateFromProps(props){
                console.log('getDeriveStateFromProps',props);
                return props
            }

            render(){
                return(
                    <div>
                        <h2>数据和为：{this.state.count}</h2>
                        <h3>姓名：{this.state.name}</h3>
                        <button onClick={this.addCount}>点击+1</button>
                    </div>
                )
            }
        }
        ReactDOM.render(<Count name={'李四'}/>,document.getElementById('app'))
    </script>
</body>

</html>